package com.gxa.learning.pc.service.impl;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Course;
import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.core.pojo.ProjectCourse;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.pc.mapper.CourseMapper;
import com.gxa.learning.pc.mapper.ProjectCourseMapper;
import com.gxa.learning.pc.mapper.ProjectDetailsMapper;
import com.gxa.learning.pc.mapper.ProjectMapper;
import com.gxa.learning.pc.service.ProjectCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProjectCourseServiceImpl extends BaseServiceImpl<ProjectCourse> implements ProjectCourseService {
    @Autowired
    private ProjectCourseMapper projectCourseMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ProjectDetailsMapper projectDetailsMapper = null;


    @Override
    public void addProjectCourse(List<Long> cids, Long pid) throws BusinessException {
        if (pid == null || pid == 0 || cids == null || cids.size() == 0) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        //判断需要添加的课程是否存在
        Map<String, Object> map = projectDetailsMapper.inUseProject(pid);
        if (map == null || map.size() == 0) {
            throw new BusinessException(ExceptionCode.WRONG_PROJECT);
        }

        //删除所有选择的课程
        projectCourseMapper.deleteAllCourse(pid);

        //给要添加的课程以及项目赋值关联
        ArrayList<ProjectCourse> projectCourses = new ArrayList<>();
        for (Long cid : cids) {
            //先查询课程是否存在
            if (courseMapper.selectByPrimaryKey(cid) == null) {
                throw new BusinessException(ExceptionCode.NO_SUCH_OBJ);
            }
            //创建一个待加入的对象
            ProjectCourse projectCourse = new ProjectCourse();
            projectCourse.setProjectId(pid);
            //如果对象存在就添加记录到项目课程的关联表projectCourse表
            projectCourse.setCourseId(cid);
            projectCourses.add(projectCourse);
        }
        //加入新的课程
        projectCourseMapper.batchInsert(projectCourses);
    }

    @Override
    public List<Course> getCourseList(Long pid) throws BusinessException {
        if (pid == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        Project project = projectMapper.selectByPrimaryKey(pid);
        if (project == null) {
            throw new BusinessException(ExceptionCode.NO_SUCH_OBJ);
        }
        List<Course> courses = projectCourseMapper.getCourses(pid);
        if (courses == null || courses.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_CHOOSE_COURSE);
        }
        return courses;
    }

    @Override
    public List<Course> getAllCourse() throws BusinessException {
        List<Course> allCourse = courseMapper.getAllCourse();
        if (allCourse == null || allCourse.size() == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DATA);
        }
        return allCourse;
    }

}
